package lionel.MyController;

import java.io.IOException;

import wiiremotej.WiiRemote;
import wiiremotej.WiiRemoteExtension;
import wiiremotej.event.WRAccelerationEvent;
import wiiremotej.event.WRButtonEvent;
import wiiremotej.event.WRCombinedEvent;
import wiiremotej.event.WRExtensionEvent;
import wiiremotej.event.WRIREvent;
import wiiremotej.event.WRStatusEvent;
import wiiremotej.event.WiiRemoteListener;

public class MyControllerJWiimoteImpl extends MyController implements WiiRemoteListener{
	static org.apache.log4j.Category log = org.apache.log4j.Category.getInstance(MyControllerJWiimoteImpl.class.getName());
	
	private static final int VIBRATE_DELAY_ESTOP = 500;
	
	private int OneCount=0, TwoCount=0;
	WiiRemote remote;
	
	public MyControllerJWiimoteImpl(String id, WiiRemote rem) {
		super(id);
		remote = rem;
	}	
	
	public void buttonInputReceived(WRButtonEvent evt) {
		log.debug("buttonInputReceived");
		if (m_browser == null) return;
		if (evt.wasReleased(WRButtonEvent.HOME)) 
		{ 
			OneCount = 0; 
			TwoCount = 0; 
			m_browser.select();
		}
		if (evt.wasPressed(WRButtonEvent.RIGHT)) 	// next roster
		{ m_browser.lst1Next();	OneCount = 0; TwoCount = 0; }
		if (evt.wasPressed(WRButtonEvent.LEFT)) 	// prev roster
		{ m_browser.lst1Prev();	OneCount = 0; TwoCount = 0; }
		if (evt.wasPressed(WRButtonEvent.UP))	// previous running roster
		{ m_browser.lst2Next();	OneCount = 0; TwoCount = 0; }
		if (evt.wasPressed(WRButtonEvent.DOWN)) 	// next running roster
		{ m_browser.lst2Prev();	OneCount = 0; TwoCount = 0; }
		if (m_conductor == null) return;
		if ( (evt.isPressed(WRButtonEvent.ONE) && evt.isPressed(WRButtonEvent.TWO)) )  // E-stop
		{
			m_conductor.locEStop();
			try {
				evt.getSource().vibrateFor(VIBRATE_DELAY_ESTOP);
			} catch (IOException e) {
				log.error("Exception while vibrating: "+ e.toString());
				e.printStackTrace();
			}
		}
		else
		{
			if (evt.wasPressed(WRButtonEvent.TWO))  // slow - idle
			{
				log.debug("Button two, TwoCount is "+TwoCount);
				if (TwoCount == 0)
					{ m_conductor.locSlow(); TwoCount=1; }
				else if (TwoCount == 1)
					{ m_conductor.locStop(); TwoCount=0; }
				 OneCount = 0;
			}
			if (evt.wasPressed(WRButtonEvent.ONE)) 	// cruise - max
			{
				log.debug("Button two, OneCount is "+OneCount);
				if (OneCount == 0)
					{ m_conductor.locCruise(); OneCount=1; }
				else if (OneCount == 1)
					{ m_conductor.locMax(); OneCount=0; }
				TwoCount=0;
			}
		}
		if (evt.isPressed(WRButtonEvent.A)) 	// Speed -- loop start
			{ m_conductor.locAcc(false); OneCount = 0; TwoCount = 0; }
		if (evt.isPressed(WRButtonEvent.B)) 	// Speed ++ loop start
			{ m_conductor.locAcc(true); OneCount = 0; TwoCount = 0; }
		if ( (evt.wasReleased(WRButtonEvent.A)) || (evt.wasReleased(WRButtonEvent.B)) )	//Speed loop stop
			{ m_conductor.locAccStop(); OneCount = 0; TwoCount = 0; }
		if (evt.wasPressed(WRButtonEvent.PLUS))		// Direction
			{ m_conductor.locDir(true); OneCount = 0; TwoCount = 0; }
		if (evt.wasPressed(WRButtonEvent.MINUS))
			{ m_conductor.locDir(false); OneCount = 0; TwoCount = 0; }     
//		if (evt.wasPressed(WRButtonEvent.HOME))     // Lights
//			{ m_conductor.locSwitchLight(); OneCount = 0; TwoCount = 0; }
	}


	public void disconnected() {
		log.error("WiiRemote disconnected...");
		m_conductor.locAccStop();	
	}

	public void IRInputReceived(WRIREvent evt) {
		// TODO Auto-generated method stub
	}
	public void statusReported(WRStatusEvent evt) {		
		// TODO Auto-generated method stub
	}
	public void combinedInputReceived(WRCombinedEvent evt) {
		// TODO Auto-generated method stub		
	}	
	public void accelerationInputReceived(WRAccelerationEvent evt) {
		// TODO Auto-generated method stub	
	}
	public void extensionConnected(WiiRemoteExtension extension) {
		// TODO Auto-generated method stub
	}
	public void extensionDisconnected(WiiRemoteExtension extension) {
		// TODO Auto-generated method stub	
	}
	public void extensionInputReceived(WRExtensionEvent evt) {
		// TODO Auto-generated method stub	
	}
	public void extensionPartiallyInserted() {
		// TODO Auto-generated method stub		
	}
	public void extensionUnknown() {
		// TODO Auto-generated method stub
	}
	
	public void shutdown()
	{
		try
		{
			if (remote.isSpeakerEnabled())
				remote.stopSound();
			if (remote.isVibrating())
				remote.stopVibrating();
		} catch (Exception e) {
			log.warn("Problem will shuting down wiimote, proceeding anyway");
		} finally
		{
			remote.disconnect();
		}
			
	}
}
